<template>
  <div class="divBox relative">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container mt-1">
          <el-form inline size="small">
            <el-button type="primary" size="mini" @click="addBtn">新增</el-button>
          </el-form>
        </div>
      </div>
      <el-table
        :data="list"
        style="width: 100%"
        size="mini"
        height="500px"
        :highlight-current-row="true"
        :header-cell-style=" {fontWeight:'bold'}"
      >
        <el-table-column
          prop="id"
          label="ID"
          min-width="50"
        />
        <el-table-column label="部门"
                         prop="job_name"
                         min-width="100"
                         :show-overflow-tooltip="true">
        </el-table-column>
        <el-table-column
          label="添加时间"
          min-width="120"
          align="center"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.add_time }}</span>
          </template>
        </el-table-column>
		<el-table-column
		  label="更新时间"
		  min-width="120"
		  align="center"
		>
		  <template slot-scope="scope">
		    <span>{{ scope.row.update_time }}</span>
		  </template>
		</el-table-column>
		
        <el-table-column label="操作" min-width="130" fixed="right">
          <template slot-scope="scope">
			<el-button type="text" @click="editRow(scope.row)">编辑</el-button>
			<el-button type="text" @click="delRow(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
	  <div class="page">
	              <el-pagination
	                      background
	                      @size-change="pageChange"
	                      @current-change="handleCurrentChange"
	                      :current-page="page"
	                      :page-size="pageSize"
	                      layout="total, sizes, prev, pager, next, jumper"
	                      :total="page_totle">
	              </el-pagination>
	          </div>
    </el-card>
	
	<!-- 新增角色 -->
	<el-dialog
	  :title="title"
	  :visible.sync="formShow"
	  :close-on-click-modal="false"
	  width="500px">
	  <el-form ref="form" :model="form" label-width="120px">
		  <el-form-item label="部门名称">
			<el-select v-model="form.qywx_dep_id" size="small" v-if="depList.length" placeholder="请选择">
			    <el-option
			      v-for="item in depList"
			      :key="item.id"
			      :label="item.name"
			      :value="item.id">
			    </el-option>
			  </el-select>
			  <el-input v-if="!depList.length" size="small" v-model="form.job_name"></el-input>
		  </el-form-item>
	  </el-form>
	  <span slot="footer" class="dialog-footer">
	    <el-button size="mini" @click="formShow = false">取 消</el-button>
	    <el-button size="mini" type="primary" @click="confirm">确 定</el-button>
	  </span>
	</el-dialog>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        title:'新增',
		isShow:false,
		form:{
			id:null,
			job_name:"",
			qywx_dep_id:''
		},
		list:[],
		page:1,
		pageSize:10,
		table_name:"shop_job",
		depList:[]
      }
    },
    mounted() {
	  this.getDepList()
      this.lodeList()
    },
    methods: {
		getDepList(){
			this.$api.post('/sadmin/getDepList',{}).then(res=>{
				  if(res.code==200){
					  let item = res.data
					  if(item.errcode==0){
						  this.depList = item.department
					  }
				  }
			})
		},
		editRow(row){
			this.addTitle = '编辑'
			for(let i in this.form){
				this.form[i] = row[i]
			}
			this.formShow = true
		
		},
		addBtn(){
			this.formShow = true
			this.form = {
				id:null,
				job_name:"",
				qywx_dep_id:''
			}
		},
		confirm(){
			    if(this.form.qywx_dep_id){
					for(let item of this.depList){
						if(item.id == this.form.qywx_dep_id){
							this.form.job_name = item.name
						}
					}
				}
		        let params = {
		        	dd:this.form,
		        }
				this.$api.post('/sadmin/addEditDep',params).then(res=>{
					  if(res.code==200){
					  	this.$message.success('操作成功')
					  	this.formShow = false
					  	this.lodeList()
					  }
				})
		},
		delRow(id){
			this.$confirm('确定删除', '提示', {
					  confirmButtonText: '确定',
					  cancelButtonText: '取消',
					  type: 'warning'
					}).then(() => {
						let params = {
							dd:{id},
						}
					  this.$api.post('/sadmin/delDep',params).then(res=>{
					  	  if(res.code==200){
					  	  	this.lodeList()
					  	  }
					  })
			  })
	   },
      lodeList(){
		  let params = {
		          	page:this.page,
		          	pageSize:this.pageSize,
		    }
		  this.$api.post('/sadmin/getDeptList',params).then(res=>{
			  let data = res.data
			  this.page_totle = data.count
			  this.list = data.result
		  })
	  }
      
    }
  }
</script>
